package com.example.gongming.fenshux;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class Fenshu extends AppCompatActivity {

    EditText num1_fenzi,num1_fenmu;
    EditText num2_fenzi,num2_fenmu;
    EditText result_fenzi,result_fenmu;

    Button op;
    Button calc;
    Button btngo;
    Button btnClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fenshu);

        num1_fenzi=(EditText)findViewById(R.id.num1_fenzi);
        num1_fenmu=(EditText)findViewById(R.id.num1_fenmu);
        num2_fenzi=(EditText)this.findViewById(R.id.num2_fenzi);
        num2_fenmu=(EditText)this.findViewById(R.id.num2_fenmu);
        result_fenzi=(EditText)findViewById(R.id.result_fenzi);
        result_fenmu=(EditText)findViewById(R.id.result_fenmu);

        op=(Button)findViewById(R.id.btn_op);
        calc=(Button)findViewById(R.id.btn_calc);

        btngo=(Button)findViewById(R.id.btn_guoqu);
        btnClear=(Button)findViewById(R.id.btn_clear);


        btngo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1_fenzi.setText(result_fenzi.getText().toString());
                num1_fenmu.setText(result_fenmu.getText().toString());
            }
        });

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1_fenzi.setText("");
                num1_fenmu.setText("");

                num2_fenzi.setText("");
                num2_fenmu.setText("");
            }
        });

        op.setText("+");

        op.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ops="+-*/";

                String curop=op.getText().toString();

                int index=ops.indexOf(curop);

                index++;

                if(index==4)
                    index=0;

                op.setText(ops.substring(index,index+1));
            }
        });

        calc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isEmpty(num1_fenzi) || isEmpty(num1_fenmu) || isEmpty(num2_fenzi) || isEmpty(num2_fenmu)){
                    new AlertDialog.Builder(Fenshu.this)
                            .setTitle("Message")
                            .setMessage("Please fill all the fenzi and fenmu")
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();

                    return;
                }


                GFenshu f1=new GFenshu(num1_fenzi.getText().toString(),num1_fenmu.getText().toString());

                GFenshu f2=new GFenshu(num2_fenzi.getText().toString(),num2_fenmu.getText().toString());

                String op_text=op.getText().toString();

                op_text = op_text.trim();

                GFenshu f3=new GFenshu(1,1);

                switch (op_text)
                {
                    case "+":
                        f3=f1.add(f2);
                        break;
                    case "-":
                        f3=f1.sub(f2);
                        break;
                    case "*":
                        f3=f1.cheng(f2);
                        break;
                    case "/":
                        f3=f1.chu(f2);
                        break;

                }


                f3.Jianhua();;
                result_fenzi.setText(String.valueOf(f3.getFenzi()));
                result_fenmu.setText(String.valueOf(f3.getFenmu()));

                //result_fenmu.setText(op_text);
            }
        });
    }

    private boolean isEmpty(EditText et){
        String s=et.getText().toString();

        return s.trim().length()==0;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fenshu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
